package org.flashbrighton.as3bots.models
{
	import __AS3__.vec.Vector;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Dictionary;
	
	import org.flashbrighton.as3bots.bots.BaseBot;
	import org.flashbrighton.as3bots.events.DeadBotEvent;
	
	
	public class GameModel extends EventDispatcher
	{		
		private var _isPlaying:Boolean;
		private var _bots:Vector.<BaseBot>;
		private var _botViews:Dictionary;
		private var _walls:Array;
		private var _wallViews:Dictionary;
		private var _physics:Physics;
		
		
		public function GameModel()
		{
			init();
			createWorld();
		}
		
		
		private function init():void
		{
			_bots = new Vector.<BaseBot>();
			_botViews = new Dictionary();
			_walls = new Array();
			_wallViews = new Dictionary();
			_isPlaying = false;
		}
		
		
		private function createWorld():void
		{
			_physics = new Physics(800, 500);
		}
		
		
		public function get isPlaying():Boolean
		{
			return _isPlaying;
		}
		
		
		public function set isPlaying(val:Boolean):void
		{
			_isPlaying = val;
		}
		
		
		public function get physics():Physics
		{
			return _physics;
		}
		
		
		public function get botList():Vector.<BaseBot>
		{
			return _bots;
		}
		
		
		public function get botViews():Dictionary
		{
			return _botViews;
		}
		
		
		public function addBot(bot:BaseBot, view:Sprite):void
		{
			_bots.push(bot);
			_botViews[bot] = view;
		}
		
		
		public function removeBot(bot:BaseBot):void
		{
			_bots.splice(_bots.indexOf(bot), 1);
			_physics.world.destroyBody(bot.model.body);
			dispatchEvent(new DeadBotEvent(DeadBotEvent.DEAD, bot));
		}
		
				
		public function updateWorld():void
		{
			_physics.step();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
	}
}